// pages/about/about.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    gangwei:null,
    companyList: {},
    paperName: '',
    is_null: true,
    selectOption: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadUserInfo()
  },

  //搜索框变化
  searchValueOnChange: function (e) {
    this.setData({
      paperName:e.detail.value,
    })
  },
  //搜索
  search: function(override){
    var that = this;
    try {
      var empinfo = wx.getStorageSync('empinfo')
      //console.log('人员ID'+empinfo.employeeId)
      if (empinfo){
          app.formPost('/wx/user/select-company',{
            companyName: that.data.paperName,
            companyId: empinfo.companyId,
            
          }).then(res=>{
              if(res.code == 200){
              //console.log(res.companyList)
              that.setData({
                companyList: res.companyList
              })
              if(res.companyList.length > 0){
                that.setData({
                  is_null: false
                })
              }
            }else{
              wx.showToast({
                title: res.msg,
                icon: 'error',
                duration: 2000
              })
            }
          }).catch(e => {
            wx.showModal({
              title: e
            })
          })
      }else{
        wx.reLaunch({
          url: '/pages/user/bind/index',
        });
      }
    }catch (e) {
      wx.reLaunch({
        url: '/pages/user/bind/index',
      });
    } 
  },
   //单选变更
   radioChange: function (e) {
    //console.log('单选变更'+e.detail.value)
    this.setData({
      selectOption: e.detail.value
    });
  },
  //重置
  reset: function (e) {
    //console.log('单选变更'+e.detail.value)
    this.setData({
      companyList: {},
      paperName: '',
      is_null: true,
      selectOption: ''
    });
  },
  /**
   * 切换公司
   */
  changetCompany: function () {
    var companyId = this.data.selectOption;
    //console.log(companyId)
    if(companyId == '' || companyId == null){
          wx.showToast({
            title: '请选择切换公司!'
          })
          return false;
    }
    wx.showModal({
      title: '提示',
      content: '切换成功后需要重新登录，确认切换吗?',
      success (res) {
        if (res.confirm) {
          try {
              var empinfo = wx.getStorageSync('empinfo')
              if (empinfo){
                  app.formPost('/wx/user/update-company',{
                    companyId: companyId,
                    employeeId: empinfo.employeeId,
                  }).then(res=>{
                    if(res.code == 200){
                      wx.reLaunch({
                        url: '/pages/user/bind/index',
                      });
                    }else{
                      wx.showToast({
                        title: res.msg,
                        icon: 'error',
                        duration: 2000
                      })
                    }
                  }).catch(e => {
                    wx.showModal({
                      title: e
                    })
                  })
              }else{
                wx.reLaunch({
                  url: '/pages/user/bind/index',
                });
              }
            }catch (e) {
              wx.reLaunch({
                url: '/pages/user/bind/index',
              });
            } 
              if(companyId == '' || companyId == null) {
                wx.showToast({
                title: '请选择切换公司!'
              })
              return false
            }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  loadUserInfo() {
    let _this = this
    var empinfo = wx.getStorageSync('empinfo')
      _this.setData({
        userInfo: empinfo,
      });
  },

  // 添加个人微信
  showQrcode: function() {
    wx.previewImage({
      current: 'https://mmbiz.qpic.cn/mmbiz_jpg/GxbBAxrsEiboiaOY2EnpjVFTGp7ia5JOux88eu1sLiciaz73WcZYEQIClqpcd2OQp9jbNW4NDuTbEuhZOToH8dtZHBw/0?wx_fmt=jpeg', // 当前显示图片的http链接
      urls: ['https://mmbiz.qpic.cn/mmbiz_jpg/GxbBAxrsEiboiaOY2EnpjVFTGp7ia5JOux88eu1sLiciaz73WcZYEQIClqpcd2OQp9jbNW4NDuTbEuhZOToH8dtZHBw/0?wx_fmt=jpeg'] // 需要预览的图片http链接列表
    })
  },

  // 拨打电话
  phoneCall: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.userInfo.phone,
      complete: (res) => {},
      fail: (res) => {},
      success: (res) => {},
    })
  },

  // 保存通讯录
  saveContact: function() {
    wx.addPhoneContact({
      firstName: this.data.userInfo.realName,
      remark: this.data.userInfo.userDeptName.replace("部","工程师"),
      mobilePhoneNumber: this.data.userInfo.phone,
      weChatNumber: '大笨笨一号',
      organization: '上海威派格智慧水务股份有限公司',
      title: this.data.userInfo.userDeptName.replace("部","工程师")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})